package codetop.bytedance.T221;

/**
 * @Author: 18362
 * @Create: 2022-09-17 16:59:03 星期六
 */
class Solution {
    public int maximalSquare(char[][] matrix) {
        int[] dp = new int[matrix[0].length+1];
        int result = 0;
        for (int i = 0; i < matrix.length; i++) {
            int last = dp[0];
            for (int j = 1; j < dp.length; j++) {
                if (matrix[i][j-1] == '1') {
                    int t = dp[j];
                    dp[j] = Math.min(last, Math.min(dp[j-1], dp[j])) + 1;
                    last = t;
                } else {
                    last = dp[j];
                    dp[j] = 0;
                }
                result = Math.max(result, dp[j]);
            }
        }
        return result * result;
    }
}